/**
 * @author:稀饭
 * @time:下午3:21:07
 * @filename:UserOperction.java
 */
package com.mybatis.test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.dao.UserMapper;
import com.mybatis.pojo.User;
/**
 * 測試類
 */
public class UserOperation {

	public static void main(String[] args) throws IOException {
		String resource = "sqlMap-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 获取SqlSessionFactory即session工厂
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
				.build(inputStream);
		// 获取session
		SqlSession session = sqlSessionFactory.openSession();
		User user = new User();
		user.setPassword("123456");
		user.setUsername("123456");
		int id = -1;
		try {
			// 由session获取UserDao映射器
			UserMapper mapper = (UserMapper) session
					.getMapper(UserMapper.class);
			id = mapper.insertSelective(user);
			user = mapper.selectByPrimaryKey(2);
		} finally {
			// 提交
			session.commit();
			session.close();
		}
		System.out.println(user.getUsername());
		System.out.println(id);
	}
}
